package com.yuda.operationcenter.controller;

import com.github.pagehelper.PageHelper;
import com.yuda.common.core.utils.StringUtils;
import com.yuda.common.core.utils.poi.ExcelUtil;
import com.yuda.common.core.utils.sql.SqlUtil;
import com.yuda.common.core.web.page.PageDomain;
import com.yuda.common.core.web.page.TableSupport;
import com.yuda.common.security.annotation.PreAuthorize;
import com.yuda.operationcenter.domain.OperProvince;
import com.yuda.operationcenter.domain.OperSite;
import com.yuda.operationcenter.domain.OperSiteParam;
import com.yuda.operationcenter.dto.Result;
import com.yuda.operationcenter.service.OperSiteService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
public class OperSiteController {

    @Autowired
    OperSiteService operSiteService;

    @PreAuthorize(hasPermi = "gwoper:apply:query")
    @GetMapping("/select/site")
    @ApiOperation("查询考点资质申请接口")
    public List<OperSite> querySite(@ApiParam("筛选参数") OperSiteParam param) {
        startPage();
        List<OperSite> operSites = operSiteService.selectSite(param);
        return operSites;
    }

    @PreAuthorize(hasPermi = "gwoper:apply:remove")
    @PostMapping("/deleteSite/{id}")
    @ApiOperation("删除考点资质申请接口")
    public Object deleteSite(@ApiParam("被删除考点的id") @PathVariable("id") Integer id) {
        int count = operSiteService.deleteSite(id);
        if(count > 0){
            return new Result().successOperation(count);
        }
        return new Result().failedOperation();
    }

    @PreAuthorize(hasPermi = "gwoper:apply:remove")
    @PostMapping("/deleteSiteMore/{ids}")
    @ApiOperation("批量删除考点资质申请接口")
    public Object deleteSiteMore(@PathVariable("ids") Integer[] ids) {
        int count = operSiteService.deleteSiteMore(ids);
        if(count > 0){
            return new Result().successOperation(count);
        }
        return new Result().failedOperation();
    }

    @PreAuthorize(hasPermi = "gwoper:apply:export")
    @GetMapping("/exportSite")
    @ApiOperation("导出考点资质申请信息Excel")
    public void exportSite(HttpServletResponse response, OperSiteParam param) throws IOException {
        List<OperSite> operSites = operSiteService.selectSite(param);
        ExcelUtil<OperSite> excelUtil = new ExcelUtil<>(OperSite.class);
        excelUtil.exportExcel(response, operSites, "考点资质申请信息表");
    }
    @GetMapping("/selectProvinceNameAndId")
    @ApiOperation("查询省份名称和id")
    public HashSet<OperProvince> selectProvinceName(){
        HashSet<OperProvince> provinceNameAndIds = operSiteService.selectProvinceName();
        return provinceNameAndIds;
    }
    @GetMapping("/selectCityName/{provinceId}")
    @ApiOperation("查询城市名称")
    public HashSet<String> selectCityName(@PathVariable("provinceId") String provinceId){
        HashSet<String> cityNames = operSiteService.selectCityName(provinceId);
        return cityNames;
    }
    /**
     * 设置请求分页数据
     */
    protected void startPage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }
}

